package com.dorm.service;

import com.dorm.entity.Building;
import com.dorm.mapper.BuildingMapper;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class BuildingService {
    private final BuildingMapper buildingMapper;

    public BuildingService(BuildingMapper buildingMapper) {
        this.buildingMapper = buildingMapper;
    }

    public List<Building> getAllBuildings() {
        List<Building> buildings = buildingMapper.findAll();
        buildings.forEach(building -> {
            if (building.getTotalRooms() == null) {
                building.setTotalRooms(0);
            }
            if (building.getOccupancyRate() == null) {
                building.setOccupancyRate(0.0);
            }
        });
        return buildings;
    }

    public Building getBuildingById(Long id) {
        return buildingMapper.findById(id);
    }

    public Building createBuilding(Building building) {
        buildingMapper.insert(building);
        return building;
    }

    public Building updateBuilding(Building building) {
        buildingMapper.update(building);
        return building;
    }

    public void deleteBuilding(Long id) {
        buildingMapper.deleteById(id);
    }
} 